/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.system;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.grayrabbit.cms.action.common.BaseAction;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.entity.Config;
import org.grayrabbit.cms.entity.Dictionary;
import org.grayrabbit.cms.service.system.ConfigService;
import org.grayrabbit.cms.vo.system.ConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;

/**
 * <b>ConfigAction</b> Struts2 action�?





 * @version 1.0,创建时间:2011-05-16 15:55:00 
 */
@Controller
@Scope("prototype")
@Results( {
		@Result(name = "reload", type = "redirect", location = "config-list.action"),
		@Result(name = "input", type = "dispatcher", location = "config-input.jsp"),
		@Result(name = "success", type = "dispatcher", location = "config-list.jsp")
		}
		)
public class ConfigAction extends BaseAction<ConfigBean> {
	
	private static final long serialVersionUID = -1L;
	//Service接口接入
	@Autowired
	@Qualifier("configServiceImpl")
	private ConfigService configService;
	
	private List<Dictionary> isActiveList;
	//bean 对象List
	private List<ConfigBean> configList;
	
	private List<Config> userConfigList;//用户配置
	private List<Config> emailConfigList;//邮件配置
	private List<Config> commentConfigList;  //评论配置
	private List<Config> attachConfigList; //附件配置
	
	//VO对象，由前端页面数据组装
	private ConfigBean config;
	//分页对象
	private Page<Config> page = new Page<Config>(MAX_PAGE_NUM,true);
	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(config==null){
			config = new ConfigBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public ConfigBean getModel() {
		return config;
	}
	
	@Override
	protected void prepareModel(){
		if((null!=config)&&StringUtils.isNotBlank(config.getConfigId())){
			config = (ConfigBean)dozer.map(configService.getConfig(config.getConfigId()),ConfigBean.class);
		}
	}

	/**
	 * 编辑保存配置信息
	 * @return
	 */
	@Action(value="config-update") 
	public String batchUpdate(){
		configService.batchUpdate(userConfigList);
		configService.batchUpdate(emailConfigList);
		configService.batchUpdate(commentConfigList);
		configService.batchUpdate(attachConfigList);
		return RELOAD;
	}
	/**
	 * 删除<b>Config</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到config-list.action
	 */
	@Override
	public String delete(){
		configService.delete(key);
		return RELOAD;
	}
	
	/**
	 * Config的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到config-list.jsp页面
	 */
	@Override
	@Action(value="config-list")
	public String list(){
		ConfigBean conf=null;
		
		conf=new ConfigBean();
		conf.setConfigType("sys_User_Config");
		userConfigList=configService.getConfigs(conf);
		
		conf=new ConfigBean();
		conf.setConfigType("e_Mail_Config");
		emailConfigList=configService.getConfigs(conf);
		
		conf=new ConfigBean();
		conf.setConfigType("comment_Config");
		commentConfigList=configService.getConfigs(conf);
		
		conf=new ConfigBean();
		conf.setConfigType("attach_Config");
		attachConfigList=configService.getConfigs(conf);
		
		isActiveList = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_CONFIG);
		return SUCCESS;
	}
	
	/**
	 * save or update Config的方法，将成员property config转成Config实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到config-list.action
	 */
	@Override
	public String save(){
		Config obj = (Config) dozer.map(config, Config.class);
		if(StringUtils.isBlank(obj.getConfigId())){
			obj.setConfigId(null);
		}
		configService.save(obj);
		return RELOAD;
	}
	
	public List<Dictionary> getIsActiveList() {
		return isActiveList;
	}

	public void setIsActiveList(List<Dictionary> isActiveList) {
		this.isActiveList = isActiveList;
	}
	
	/**return 前端页面数据组装VO对象,<b>ConfigBean</b>*/
	public ConfigBean getConfig() {
		return config;
	}

	public void setConfig(ConfigBean config) {
		this.config = config;
	}

	public List<ConfigBean> getConfigList() {
		return configList;
	}

	public void setConfigList(List<ConfigBean> configList) {
		this.configList = configList;
	}
	
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<Config> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<Config> page) {
		this.page = page;
	}


	public List<Config> getUserConfigList() {
		return userConfigList;
	}

	public void setUserConfigList(List<Config> userConfigList) {
		this.userConfigList = userConfigList;
	}

	public List<Config> getEmailConfigList() {
		return emailConfigList;
	}

	public void setEmailConfigList(List<Config> emailConfigList) {
		this.emailConfigList = emailConfigList;
	}

	public List<Config> getCommentConfigList() {
		return commentConfigList;
	}

	public void setCommentConfigList(List<Config> commentConfigList) {
		this.commentConfigList = commentConfigList;
	}

	public List<Config> getAttachConfigList() {
		return attachConfigList;
	}

	public void setAttachConfigList(List<Config> attachConfigList) {
		this.attachConfigList = attachConfigList;
	}
	
	
}